package cn.ad.server.web.servlet;

import cn.ad.server.web.servlet.v2.RestPathFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Server2Server 广告请求Servlet
 * @author dongbin.yu
 * @from 2016-05-18
 * @since V1.0
 * @deprecated 原广告请求处理Servlet框架将不推荐使用，统一采用v2包下的处理方式，请求入口参考{@link RestPathFilter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)}
 */
@Deprecated
@WebServlet("/api.html")
public class STSAdRequestServlet extends AbstractAdServlet {

	private static final long serialVersionUID = 8306491851352145719L;
	private final static Logger logger = LoggerFactory.getLogger(STSAdRequestServlet.class);

	@Override
	public void doService(HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.getRequestDispatcher("/v2/sts/ch/1").forward(request, response);
		} catch (ServletException e) {
			logger.error("ServletException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
		}
		return;
		// 调用参数解析
		// json格式解析，采用jackson来解析
//<<<<<<< .merge_file_P0bZuy
//		try(ServletInputStream inputStream = request.getInputStream();
//			ServletOutputStream outputStream = response.getOutputStream()) {
//			STSAdRequest adRequest = objectMapper.readValue(inputStream, STSAdRequest.class);
//=======
//		ServletInputStream inputStream = null;
//		ServletOutputStream outputStream = null;
//		try{
//			inputStream = request.getInputStream();
//			outputStream = response.getOutputStream();
//			STSAdRequest adRequest = objectMapper.readValue(inputStream, STSAdRequest.class);
//
//			// 执行选择默认创意的流程
//			STSAdResponse adResponse = new STSAdResponse();
//
//			// 设置一些前置返回
//			adResponse.setVersion(adRequest.getVersion());
//			adResponse.setExtendData(adRequest.getExtendData());
//			try {
//				// 解析ip，浏览器等的一些行为
//				new AdContextBuilder(adRequest, request).buildAdContext();
//
//				adProcessor.process(adRequest, adResponse);
//				
//			} catch (AdServerMessageException e) {
//				adResponse.setStatus(e.getReturnCode().getCode());
//				adResponse.setMessage(e.getMessage());
//			} catch (Exception e) {
//				logger.error(e.getMessage(), e);
//				adResponse.setStatus(ReturnCode.E1.getCode());
//				adResponse.setMessage(ReturnCode.E1.getMessage());
//			}
//
//			// 处理返回值
//			response.setContentType("application/json;charset=utf-8");
//			objectMapper.writeValue(outputStream, adResponse);
//
//		} catch (IOException e) {
//			logger.error("request param get failed", e);
//		} finally {
//			if (inputStream != null) {
//				try {
//					inputStream.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//
//			if (outputStream != null) {
//				try {
//					outputStream.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
//	}
	}
}
